package com.starblink.store.recommend.ui

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.user.UserDataCenter
import com.starblink.rocketreserver.FetchSubscribeMerchantWebRecommendQuery
import com.starblink.rocketreserver.fragment.MerchantWebVOF
import com.starblink.rocketreserver.type.MerchantWebRecommendReq
import kotlinx.coroutines.launch

open class RecommendStoreVM : BaseViewModel() {

    var myPageNo = 1
    var pageNo = 1
    var loadedRecommend = false
    var barIsWhite: MutableLiveData<Boolean> = MutableLiveData() //是否是初始状态白色
    var showViewMore = MutableLiveData(false) //顶部我的订阅是否有更多

    //首页顶部订阅店铺数据
    var myList: MutableLiveData<MutableList<MerchantWebVOF>> = MutableLiveData(mutableListOf())
    var queryMyList: MutableList<MerchantWebVOF> = mutableListOf()

    //底部推荐店铺数据
    var recommendList: MutableLiveData<MutableList<MerchantWebVOF>> =
        MutableLiveData(mutableListOf())
    var recPositionStart = 0
    var recItemCount = 0

    fun loadMyRecommendStores(showLoading: Boolean = false) {
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                FetchSubscribeMerchantWebRecommendQuery(
                    merchantParams = MerchantWebRecommendReq(
                        accountId = Optional.present(UserDataCenter.getAccountInfo()?.id ?: null),
                        countryCode = Optional.present(null),
                        2,
                        System.currentTimeMillis().toString(),
                        1,
                        100,
                    )
                ), showDefaultLoading = showLoading
            )
//            Log.d(
//                "RecommendStoreActivity",
//                "-----${response?.data?.fetchSubscribeMerchantWebRecommend?.merchantWebList}"
//            )
            response?.data?.fetchSubscribeMerchantWebRecommend?.merchantWebList?.let { it ->
                val list = it.mapNotNull { it.merchantWebVOF }
                if (queryMyList.isEmpty()) {
                    queryMyList.addAll(list)
                    for (index in 0..4) {
                        if (list.size > index)
                            myList.value!!.add(list[index])
                    }
                    showViewMore.value = list.size > 5
                } else {
                    queryMyList.clear()
                    myList.value?.clear()

                    queryMyList.addAll(list)
                    myList.value?.addAll(list)
                }
                myList.value = myList.value

            }
        }
    }

    fun showAllMyStore() {
        showViewMore.value = false
        myList.value?.clear()
        myList.value?.addAll(queryMyList)
        myList.value = myList.value
    }


    fun loadRecommendStores(sceneCode: Int = 3, showLoading: Boolean = false) {
        viewModelScope.launch {
            val response = SkGqlClient.skQuery(
                FetchSubscribeMerchantWebRecommendQuery(
                    merchantParams = MerchantWebRecommendReq(
                        accountId = Optional.present(null),
                        countryCode = Optional.present(null),
                        sceneCode,
                        System.currentTimeMillis().toString(),
                        pageNo,
                        20,//pageSize不能随意改动，adapter根据长度自动加载更多
                    )
                ), showDefaultLoading = showLoading && pageNo == 1
            )
            loadedRecommend = true
            var list: List<MerchantWebVOF> = listOf()
            response?.data?.fetchSubscribeMerchantWebRecommend?.merchantWebList?.let { it ->
                list = it.mapNotNull { it.merchantWebVOF }
                if (list.isNotEmpty()) {
                    recItemCount = list.size
                    recPositionStart = recommendList.value?.size ?: 0
                    pageNo++
                }
                recommendList.value!!.addAll(list)
                recommendList.value = recommendList.value
            }
            if (pageNo == 1 && list.isEmpty()) {
                loadNoData()
            }
        }
    }

    //其它地方订阅店铺 同步增加
    fun addStore(merchant: MerchantWebVOF) {
        queryMyList.add(0, merchant)
        myList.value?.add(0, merchant)
        myList.value = myList.value
    }

}